﻿Public Class EditUser
    Public l As New Label
    Public rid As Int16
    Private Sub EditUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadrole()
        If rid = 0 Then
            rid = 1
        End If
        cbor.SelectedValue = rid
    End Sub
    Private Sub loadrole()
        cbor.DisplayMember = "UserRoleName"
        cbor.ValueMember = "UserRoleId"
        cbor.DataSource = cmdp.ExecuteDataTable("SELECT * FROM UserRole")
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click

        cmdp.Parameters("@name") = txtname.Text
        cmdp.Parameters("@pwd") = txtpwd.Text
        cmdp.Parameters("@role") = cbor.SelectedValue

        If txtname.Text.Trim = "" Then
            MessageBox.Show("ឈ្មោះអ្នកប្រើប្រាស់មិនអាចទទេបានទេ")
            txtname.Focus()
            Exit Sub
        End If
        If txtpwd.Text.Trim = "" Then
            MessageBox.Show("លេខសម្ងាត់មិនអាចទទេបានទេ")
            txtpwd.Focus()
            Exit Sub
        End If

        If l.Text = "" Then
            cmdp.ExecuteNonQuery("INSERT INTO [User] ([Username],[Password],UserRoleId) VALUES(@name,@pwd,@role)")

        Else
            cmdp.Parameters("@id") = l.Text
            cmdp.ExecuteNonQuery("UPDATE [User] SET [Username]=@name,[Password]=@pwd,[UserRoleId]=@role WHERE UserId=@id")
            ' MessageBox.Show("កែប្រែបានសម្រេច")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub
End Class